package cn.iocoder.yudao.module.hqchart.controller.eastMoney;

import cn.iocoder.yudao.module.data.entity.dto.StockTrendsReqDTO;
import cn.iocoder.yudao.module.data.entity.dto.StockTrendsRespDTO;
import cn.iocoder.yudao.module.data.service.IEastMoneyService;
import cn.iocoder.yudao.module.hqchart.controller.vo.HQChartFenShiReqVO;
import cn.iocoder.yudao.module.hqchart.controller.vo.HQChartFenShiRespVO;
import cn.iocoder.yudao.module.hqchart.convert.EastMoneyConvert;
import cn.iocoder.yudao.module.hqchart.pojo.StockHQChartResultVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * </p>
 *
 * @author pokbrin
 * @date 2023-07-18 17:01
 */
@Slf4j
@Tag(name = "东财-转HQChart接口")
@RestController
@RequestMapping("api/east-money")
public class EastMoneyController {
    @Resource
    private ThreadPoolTaskExecutor taskExecutor;
    @Resource
    private IEastMoneyService eastMoneyService;

    @Resource
    private EastMoneyConvert eastMoneyConvert;

    @PostMapping("/stock/trends")
    @Operation(summary = "股票走势-")
    public StockHQChartResultVO<List<HQChartFenShiRespVO>> stockTrends(HQChartFenShiReqVO vo) {
        Set<String> symbols = vo.getSymbol();
        List<CompletableFuture<HQChartFenShiRespVO>> futures = symbols.stream().map(symbol -> CompletableFuture.supplyAsync(() -> {
            StockTrendsReqDTO dto = new StockTrendsReqDTO();
            dto.setCode(symbol);
            StockTrendsRespDTO respDTO = eastMoneyService.stockTrends(dto);
            return eastMoneyConvert.eastMoneyTrends2HQChartVO(respDTO);
        }, taskExecutor)).collect(Collectors.toList());
        List<HQChartFenShiRespVO> respVos = futures.stream().map(CompletableFuture::join).collect(Collectors.toList());
        return StockHQChartResultVO.success(respVos);
    }


}
